package com.wyw.design.model.learning.behavioral.memo;

import java.util.ArrayList;
import java.util.List;

/**
 * @Title   Caretaker
 * @Description
 * @Author Mr Wu yewen.wu.china@gmail.com
 * @Date 2023/4/20 11:16
 * Update History:
 * Author        Time            Content
 */
public class BankAccountManager {
    private List<Memento> mementos = new ArrayList<>();
    private BankAccount account;

    public BankAccountManager(BankAccount account) {
        this.account = account;
    }

    public void deposit(int amount) {
        account.deposit(amount);
        mementos.add(account.save());
    }

    public void withdraw(int amount) {
        account.withdraw(amount);
        mementos.add(account.save());
    }

    public void undo() {
        if (mementos.isEmpty()) {
            return;
        }
        Memento memento = mementos.remove(mementos.size() - 1);
        account.restore(memento);
    }
}
